process.env.TZ = 'Asia/Shanghai';
const sendMail = require("./utils/mail");
const schedule = require("node-schedule");

const getPublicIP = require("./tasks/getPublicIP");
const taskList = [getPublicIP];
console.log('schedule run time:',new Date())
/**
 * 发送邮件 定时任务
 * 每天的第0小时0分钟的第1秒定时执行一次:
 * 取消定时器: schedule.cancel();
 * https://www.npmjs.com/package/node-schedule
  //year
  //dayOfWeek(0-6)
  //month(0-11)
  //date(1-31)
  //hour(0-23)
  //minute(0-59)
  //second(0-59)
 */
const fire = () => {
  taskList.map((task) => {
    const { second, minute, hour } = task.config;
    var rule = new schedule.RecurrenceRule();
    if(second){
        rule.second = second;
    }
    if(minute){
        rule.minute = minute;
    }
    if(hour){
        rule.hour = hour;
    }

    const title = `Task [${task.name}]`;
    console.log(`${title} 已加载`);
    schedule.scheduleJob(rule, async () => {
      console.log(`${title} 开始执行, time ${new Date()}`);
      task
        .run()
        .then((res) => {})
        .catch((err) => {
          console.error("任务执行失败：", err);
          sendMail({
            subject: `NAS - ${title} 执行失败`,
            content: res,
          });
        });
    });
  });
};
module.exports = fire